package com.sam.im.heiqun.uis.activities;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sam.im.heiqun.R;
import com.sam.im.heiqun.entities.GroupFriendEntivity;
import com.sam.im.heiqun.entities.ImGroupEntivity;
import com.sam.im.heiqun.entities.ValidateEntivity;
import com.sam.im.heiqun.nets.PGService;
import com.sam.im.heiqun.utils.RxJavaUtil;
import com.sam.im.heiqun.utils.ToolsUtils;
import com.yuyh.library.nets.callbacks.AbsAPICallback;
import com.yuyh.library.nets.exceptions.ApiException;
import com.yuyh.library.uis.activitys.BaseSwipeBackActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.ObservableSource;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

/**
 * Created by wangguodong on 2018/1/5.
 */

public class AddGroupSetActivity extends BaseSwipeBackActivity {

    private static final int REQUESTION_CODE = 10001;

    @BindView(R.id.pre_v_back)
    ImageView preVBack;
    @BindView(R.id.ok)
    TextView ok;
    @BindView(R.id.relative_1)
    RelativeLayout relative_1;
    @BindView(R.id.relative_2)
    RelativeLayout relative_2;
    @BindView(R.id.relative_3)
    RelativeLayout relative_3;
    @BindView(R.id.relative_4)
    RelativeLayout relative_4;
    @BindView(R.id.relative_5)
    RelativeLayout relative_5;
    @BindView(R.id.relative_6)
    RelativeLayout relative_6;
    @BindView(R.id.image_select_1)
    ImageView image_select_1;
    @BindView(R.id.image_select_2)
    ImageView image_select_2;
    @BindView(R.id.image_select_3)
    ImageView image_select_3;
    @BindView(R.id.image_select_4)
    ImageView image_select_4;
    @BindView(R.id.image_select_5)
    ImageView image_select_5;
    @BindView(R.id.image_select_6)
    ImageView image_select_6;
    @BindView(R.id.image_select_7)
    ImageView image_select_7;
    @BindView(R.id.txt_qustion_3)
    TextView txt_qustion_3;
    @BindView(R.id.txt_qustion_4)
    TextView txt_qustion_4;
    @BindView(R.id.txt_resul_4)
    TextView txt_resul_4;
    @BindView(R.id.txt_fee_7_1)
    TextView txt_fee_7_1;
    @BindView(R.id.linear_3)
    LinearLayout linear_3;
    @BindView(R.id.linear_4)
    LinearLayout linear_4;
    @BindView(R.id.tv_noupgrades_id)
    TextView tv_noupgrades;

    @BindView(R.id.relative_7)
    RelativeLayout relative_7;
    @BindView(R.id.relative_7_2)
    RelativeLayout relative_7_2;
    @BindView(R.id.linear_7_1)
    LinearLayout linear_7_1;
    @BindView(R.id.linear_7_2)
    LinearLayout linear72;
    @BindView(R.id.txt_days_7_2)
    TextView txtDays72;

    private String groupId;

    private ImGroupEntivity mImGroupEntivity;

    public static void start(Activity activity, String groupId) {

        Intent intent = new Intent(activity, AddGroupSetActivity.class);
        intent.putExtra("groupId", groupId);
        activity.startActivity(intent);

    }

    @Override
    protected String getTitleText() {
        return "加群设置";
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_add_group_set;
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        ok.setText("确认");
        ok.setVisibility(View.VISIBLE);
        groupId = getIntent().getStringExtra("groupId");
//        type = getIntent().getIntExtra("type", 0);
//        quest = getIntent().getStringExtra("quest");
//        result = getIntent().getStringExtra("result");
//        joinStatus = getIntent().getStringExtra("joinStatus");

        mImGroupEntivity = ImGroupEntivity.findById(ImGroupEntivity.class, Long.parseLong(groupId));

        String joinStatus = null;
        String quest = null;
        String answer = null;
        String fee = null;
        String days = null;
        if (null != mImGroupEntivity) {
            quest = mImGroupEntivity.getQuestion();
            answer = mImGroupEntivity.getAnswer();
            joinStatus = mImGroupEntivity.getJoinStatus();
            fee = mImGroupEntivity.getFee();
            days = mImGroupEntivity.getDays();
        }

        setCheckFun(joinStatus);
        if ("2".equals(joinStatus)) {
            txt_qustion_3.setText(quest);
            linear_3.setVisibility(View.VISIBLE);
            linear_4.setVisibility(View.GONE);
            linear72.setVisibility(View.GONE);
            linear_7_1.setVisibility(View.GONE);
        } else if ("3".equals(joinStatus)) {
            txt_qustion_4.setText(quest);
            txt_resul_4.setText(answer);
            linear_3.setVisibility(View.GONE);
            linear_4.setVisibility(View.VISIBLE);
            linear72.setVisibility(View.GONE);
            linear_7_1.setVisibility(View.GONE);
        } else if ("6".equals(joinStatus)) {
//            txt_qustion_4.setText(quest);
//            txt_resul_4.setText(result);
            txt_fee_7_1.setText(fee);
            txtDays72.setText(days);
            linear_3.setVisibility(View.GONE);
            linear_4.setVisibility(View.GONE);
            linear72.setVisibility(View.VISIBLE);
            linear_7_1.setVisibility(View.VISIBLE);
        }
        if (null != mImGroupEntivity && null != mImGroupEntivity.getLevel() && !TextUtils.isEmpty(mImGroupEntivity.getLevel()) && !"0".equals(mImGroupEntivity.getLevel())) {
            relative_7_2.setVisibility(View.VISIBLE);
            tv_noupgrades.setVisibility(View.GONE);
        } else {
            tv_noupgrades.setVisibility(View.VISIBLE);
            relative_7.setEnabled(false);
            relative_7_2.setVisibility(View.GONE);
            linear72.setVisibility(View.GONE);
            linear_7_1.setVisibility(View.GONE);
        }

    }

    private void updateImageSelectState(ImageView imageView, boolean selected) {
        imageView.setImageResource(selected ? R.drawable.checkbox_partial : R.drawable.checkbox_unchecked);

    }

    private void setCheckFun(String joinStatus) {

        mImGroupEntivity.setJoinStatus(joinStatus);

        updateImageSelectState(image_select_1, "0".equals(joinStatus));
        updateImageSelectState(image_select_2, "1".equals(joinStatus));
        updateImageSelectState(image_select_3, "2".equals(joinStatus));
        updateImageSelectState(image_select_4, "3".equals(joinStatus));
        updateImageSelectState(image_select_5, "4".equals(joinStatus));
        updateImageSelectState(image_select_6, "5".equals(joinStatus));
        updateImageSelectState(image_select_7, "6".equals(joinStatus));
        image_select_1.setImageResource(R.drawable.checkbox_unchecked);
        image_select_2.setImageResource(R.drawable.checkbox_unchecked);
        image_select_3.setImageResource(R.drawable.checkbox_unchecked);
        image_select_4.setImageResource(R.drawable.checkbox_unchecked);
        image_select_5.setImageResource(R.drawable.checkbox_unchecked);
        image_select_6.setImageResource(R.drawable.checkbox_unchecked);
        image_select_7.setImageResource(R.drawable.checkbox_unchecked);
        switch (joinStatus) {
            case "0":
                image_select_1.setImageResource(R.drawable.checkbox_partial);
                break;
            case "1":
                image_select_2.setImageResource(R.drawable.checkbox_partial);
                break;
            case "2":
                image_select_3.setImageResource(R.drawable.checkbox_partial);
                break;
            case "3":
                image_select_4.setImageResource(R.drawable.checkbox_partial);
                break;
            case "4":
                image_select_5.setImageResource(R.drawable.checkbox_partial);
                break;
            case "5":
                image_select_6.setImageResource(R.drawable.checkbox_partial);
                break;
            case "6":
                image_select_7.setImageResource(R.drawable.checkbox_partial);
                break;
        }

    }


    @OnClick({R.id.ok, R.id.pre_v_back, R.id.relative_1, R.id.relative_2, R.id.relative_3, R.id.relative_4, R.id.relative_5, R.id.relative_6, R.id.relative_7})
    public void onClick(View view) {

        String joinStatus = null;
        String quest = null;
        String answer = null;
        String fee = null;
        String days = null;
        if (null != mImGroupEntivity) {
            quest = mImGroupEntivity.getQuestion();
            answer = mImGroupEntivity.getAnswer();
            joinStatus = mImGroupEntivity.getJoinStatus();
            fee = mImGroupEntivity.getFee();
            days = mImGroupEntivity.getDays();
        }

        switch (view.getId()) {
            case R.id.pre_v_back:
                scrollToFinishActivity();
                break;
            case R.id.ok:

                if ("2".equals(joinStatus)) {
                    if (null == quest || TextUtils.isEmpty(quest)) {
                        showToast("请输入问题！");
                        return;
                    }
                } else if ("3".equals(joinStatus)) {
                    if (null == quest || TextUtils.isEmpty(quest)) {
                        showToast("请输入问题！");
                        return;
                    } else if (null == answer || TextUtils.isEmpty(answer)) {
                        showToast("请输入问题的答案！");
                        return;
                    }
                } else if ("6".equals(joinStatus)) {
                    double a = 0;
                    try {
                        a = Double.parseDouble(fee);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    if (null == fee || TextUtils.isEmpty(fee) || a <= 0) {
                        showToast("请输入加群需要支付的费用！");
                    }
                }
                updateJoinstatus();
                break;
            case R.id.relative_1:
                linear_3.setVisibility(View.GONE);
                linear_4.setVisibility(View.GONE);
                linear72.setVisibility(View.GONE);
                linear_7_1.setVisibility(View.GONE);
                setCheckFun("0");
                break;
            case R.id.relative_2:
                linear_3.setVisibility(View.GONE);
                linear_4.setVisibility(View.GONE);
                linear72.setVisibility(View.GONE);
                linear_7_1.setVisibility(View.GONE);
                setCheckFun("1");
                break;
            case R.id.relative_3:
                setCheckFun("2");
                linear_4.setVisibility(View.GONE);
                linear72.setVisibility(View.GONE);
                linear_7_1.setVisibility(View.GONE);
                AddGroupSetQuestActivity.start(AddGroupSetActivity.this, 0, REQUESTION_CODE, quest, answer);
                break;
            case R.id.relative_4:
                setCheckFun("3");
                linear_3.setVisibility(View.GONE);
                linear72.setVisibility(View.GONE);
                linear_7_1.setVisibility(View.GONE);
                AddGroupSetQuestActivity.start(AddGroupSetActivity.this, 1, REQUESTION_CODE, quest, answer);
                break;
            case R.id.relative_5:
                linear_3.setVisibility(View.GONE);
                linear_4.setVisibility(View.GONE);
                linear72.setVisibility(View.GONE);
                linear_7_1.setVisibility(View.GONE);
                setCheckFun("4");
                break;
            case R.id.relative_6:
                linear_3.setVisibility(View.GONE);
                linear_4.setVisibility(View.GONE);
                linear72.setVisibility(View.GONE);
                linear_7_1.setVisibility(View.GONE);
                setCheckFun("5");
                break;
            case R.id.relative_7:
                setCheckFun("6");
                linear_3.setVisibility(View.GONE);
                linear_4.setVisibility(View.GONE);
                AddGroupSetQuestActivity.start(AddGroupSetActivity.this, 2, REQUESTION_CODE, fee, days);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case REQUESTION_CODE:
                if (resultCode == RESULT_OK) {
                    String quest = data.getStringExtra("quest");
                    String result = data.getStringExtra("result");
                    String fee = data.getStringExtra("fee");
                    String days = data.getStringExtra("days");
                    mImGroupEntivity.setQuestion(quest);
                    mImGroupEntivity.setAnswer(result);
                    mImGroupEntivity.setFee(fee);
                    mImGroupEntivity.setDays(days);
                    String joinStatus = mImGroupEntivity.getJoinStatus();

                    if ("2".equals(joinStatus)) {
                        txt_qustion_3.setText(quest);
                        linear_3.setVisibility(View.VISIBLE);
                        linear_4.setVisibility(View.GONE);
                        linear72.setVisibility(View.GONE);
                        linear_7_1.setVisibility(View.GONE);
                    } else if ("3".equals(joinStatus)) {
                        txt_qustion_4.setText(quest);
                        txt_resul_4.setText(result);
                        linear_3.setVisibility(View.GONE);
                        linear_4.setVisibility(View.VISIBLE);
                        linear72.setVisibility(View.GONE);
                        linear_7_1.setVisibility(View.GONE);
                    } else if ("6".equals(joinStatus)) {
//                        txt_qustion_4.setText(quest);
//                        txt_resul_4.setText(result);
                        txt_fee_7_1.setText(fee);
                        txtDays72.setText(days);
                        linear_3.setVisibility(View.GONE);
                        linear_4.setVisibility(View.GONE);
                        linear72.setVisibility(View.VISIBLE);
                        linear_7_1.setVisibility(View.VISIBLE);
                    }
                }
                break;
        }
    }

    /**
     *
     */
    private void updateJoinstatus() {
        Log.i("-----", "updateJoinstatus: ========groupId=" + groupId + "===uid=" + ToolsUtils.getMyUserId());

        String joinStatus = null;
        String quest = null;
        String answer = null;
        String fee = null;
        String days = null;
        if (null != mImGroupEntivity) {
            quest = mImGroupEntivity.getQuestion();
            answer = mImGroupEntivity.getAnswer();
            joinStatus = mImGroupEntivity.getJoinStatus();
            fee = mImGroupEntivity.getFee();
            days = mImGroupEntivity.getDays();
        }


        PGService.getInstance().updateJoinstatus(null == answer ? "" : answer,
                groupId,
                null == quest ? "" : quest,
                "" + joinStatus,
                ToolsUtils.getMyUserId(),
                null == fee ? "" : fee,
                null == days ? "" : days
        )
                .compose(RxJavaUtil.<ValidateEntivity>applySchedules())
                .subscribe(new AbsAPICallback<ValidateEntivity>() {
                    @Override
                    public void onNext(final ValidateEntivity friendEntivity) {
                        mImGroupEntivity.save();
                        showToast("设置成功！");
                        finish();
                    }

                    @Override
                    protected void onResultError(ApiException ex) {
                        Log.i("-----", "--001ex.getDisplayMessage()--" + ex.getDisplayMessage());
                        showToast("设置失败！");
                    }
                });
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
